package edu.study.user.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.CollectionUtils;

import edu.study.api.common.dto.BaseRespDto;
import edu.study.api.common.dto.RespDto;
import edu.study.api.common.enums.BaseResultCodeEnum;
import edu.study.user.api.dto.DepartmentUserDto;
import edu.study.user.api.dto.UserDetailDto;
import edu.study.user.api.req.departmentuser.DepartmentUserReq;
import edu.study.user.api.req.departmentuser.QueryDepartmentUserReq;
import edu.study.user.dao.mapperExt.DepartmentDOMapperExt;
import edu.study.user.dao.mapperExt.DepartmentUserDOMapperExt;
import edu.study.user.dao.mapperExt.UserDOMapperExt;
import edu.study.user.dbBean.DepartmentDO;
import edu.study.user.dbBean.DepartmentUserDO;
import edu.study.user.dbBean.UserDO;
import edu.study.user.util.UserUtils;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class DepartmentUserService {
	
	@Autowired
	private DepartmentUserDOMapperExt departmentUserDOMapperExt;

	@Autowired
	private DepartmentDOMapperExt departmentDOMapperExt;
	
	@Autowired
	private UserDOMapperExt userDOMapperExt;
	
	@Autowired
	private TransactionTemplate transactionTemplate;

	public BaseRespDto addDepartmentUser(DepartmentUserReq req) {
		BaseRespDto resp = new BaseRespDto();
		
		DepartmentDO departmentDO = departmentDOMapperExt.queryByDepartmentNo(req.getDepartmentNo());
		if(departmentDO == null) {
			log.info("DepartmentUserService.addDepartmentUser, 部门不存在! departmentNo={}", req.getDepartmentNo());
			resp.setResultFail(BaseResultCodeEnum.RECORD_NOT_FOUND);
			return resp;
		}
		
		List<DepartmentUserDO> departmentUserDOList = new ArrayList<DepartmentUserDO>(req.getUserNoList().size());
		for (String userNo : req.getUserNoList()) {
			UserDO userDO = userDOMapperExt.queryByUserNo(userNo);
			if(userDO == null) {
				log.info("DepartmentUserService.addDepartmentUser, 用户不存在! userNo={}", userNo);
				resp.setResultFail(BaseResultCodeEnum.RECORD_NOT_FOUND);
				return resp;
			}
			DepartmentUserDO newDepartmentUserDO = new DepartmentUserDO();
			newDepartmentUserDO.setDepartmentNo(req.getDepartmentNo());
			newDepartmentUserDO.setUserNo(userNo);
			UserDetailDto userDetailDto = UserUtils.getCurrentUser();
			if(userDetailDto!=null) {
				newDepartmentUserDO.setCreator(userDetailDto.getLoginName());
			}
			departmentUserDOList.add(newDepartmentUserDO);
		}
		
		Boolean flag = transactionTemplate.execute(new TransactionCallback<Boolean>() {
			@Override
			public Boolean doInTransaction(TransactionStatus arg0) {
				for (DepartmentUserDO departmentUserDO : departmentUserDOList) {
					departmentUserDOMapperExt.insertSelective(departmentUserDO);
				}
				return true;
			}
		});
		if (!flag) {
			resp.setResultFail(BaseResultCodeEnum.DB_ERROR);
			return resp;
		}
		return resp;
	}
	
	public BaseRespDto deleteDepartmentUser(DepartmentUserReq req) {
		BaseRespDto resp = new BaseRespDto();
		List<DepartmentUserDto> departmentUserListAll = new ArrayList<DepartmentUserDto>(req.getUserNoList().size());
		for (String userNo : req.getUserNoList()) {
			List<DepartmentUserDto> departmentUserList = departmentUserDOMapperExt.queryDepartmentUserList(req.getDepartmentNo(), userNo);
			if(CollectionUtils.isEmpty(departmentUserList)) {
				log.info("DepartmentUserService.deleteDepartmentUser, 部门用户不存在! departmentNo={}, userNo={}", req.getDepartmentNo(), userNo);
				resp.setResultFail(BaseResultCodeEnum.RECORD_NOT_FOUND);
				return resp;
			}
			departmentUserListAll.addAll(departmentUserList);
		}
		Boolean flag = transactionTemplate.execute(new TransactionCallback<Boolean>() {
			@Override
			public Boolean doInTransaction(TransactionStatus arg0) {
				for (DepartmentUserDto departmentUserDto : departmentUserListAll) {
					departmentUserDOMapperExt.deleteByPrimaryKey(departmentUserDto.getId());
				}
				return true;
			}
		});
		if (!flag) {
			resp.setResultFail(BaseResultCodeEnum.DB_ERROR);
			return resp;
		}
		return resp;
	}
	
	public BaseRespDto modifyDepartmentUser(DepartmentUserReq req) {
		BaseRespDto resp = new BaseRespDto();
		
		DepartmentDO departmentDO = departmentDOMapperExt.queryByDepartmentNo(req.getDepartmentNo());
		if(departmentDO == null) {
			log.info("DepartmentUserService.modifyDepartmentUser, 部门不存在! departmentNo={}", req.getDepartmentNo());
			resp.setResultFail(BaseResultCodeEnum.RECORD_NOT_FOUND);
			return resp;
		}
		
		List<DepartmentUserDO> departmentUserDOListNew = new ArrayList<DepartmentUserDO>(req.getUserNoList().size());
		for (String userNo : req.getUserNoList()) {
			UserDO userDO = userDOMapperExt.queryByUserNo(userNo);
			if(userDO == null) {
				log.info("DepartmentUserService.modifyDepartmentUser, 用户不存在! userNo={}", userNo);
				resp.setResultFail(BaseResultCodeEnum.RECORD_NOT_FOUND);
				return resp;
			}
			DepartmentUserDO newDepartmentUserDO = new DepartmentUserDO();
			newDepartmentUserDO.setDepartmentNo(req.getDepartmentNo());
			newDepartmentUserDO.setUserNo(userNo);
			UserDetailDto userDetailDto = UserUtils.getCurrentUser();
			if(userDetailDto!=null) {
				newDepartmentUserDO.setCreator(userDetailDto.getLoginName());
			}
			departmentUserDOListNew.add(newDepartmentUserDO);
		}
		List<DepartmentUserDto> departmentUserListOld = departmentUserDOMapperExt.queryDepartmentUserList(req.getDepartmentNo(), null);
		Boolean flag = transactionTemplate.execute(new TransactionCallback<Boolean>() {
			@Override
			public Boolean doInTransaction(TransactionStatus arg0) {
				if(!CollectionUtils.isEmpty(departmentUserListOld)) {
					for (DepartmentUserDto departmentUserDto : departmentUserListOld) {
						departmentUserDOMapperExt.deleteByPrimaryKey(departmentUserDto.getId());
					}
				}
				for (DepartmentUserDO departmentUserDO : departmentUserDOListNew) {
					departmentUserDOMapperExt.insertSelective(departmentUserDO);
				}
				return true;
			}
		});
		if (!flag) {
			resp.setResultFail(BaseResultCodeEnum.DB_ERROR);
			return resp;
		}
		return resp;
	}
	
	public RespDto<List<DepartmentUserDto>> queryDepartmentUserList(QueryDepartmentUserReq req) {
		RespDto<List<DepartmentUserDto>> resp = new RespDto<List<DepartmentUserDto>>();
		List<DepartmentUserDto> departmentUserList = departmentUserDOMapperExt.queryDepartmentUserList(req.getDepartmentNo(), req.getUserNo());
		resp.setData(departmentUserList);
		return resp;
	}
}
